/**
 * 订单路由
 */

const express = require('express');
const router = express.Router();
const { networkStatus } = require('../utils/constant');
const { readOrder, addOrder, deleteOrder, queryOrder } = require('../file/order');


/**
 * 更新订单
 * @param {*} data 
 * @returns 
 */
router.post('/update', async (req, res) => {
    console.log('body', req.body);
    try {
        await addOrder(req.body);
        res.send({
            code: 200,
            data: null,
            msg: networkStatus['200'],
        })
    }catch (error) {
        console.log(error);
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

/**
 * 查询所有订单
 * @param {*} data 
 * @returns 
 */
router.post('/findAll', async (req, res) => {
    console.log('body', req.body);
    try {
        const data =  await readOrder(req.body);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    }catch (error) {
        console.log(error);
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

// 删除订单
router.post('/delete', async (req, res) => {
    try {
        const data = await deleteOrder(req.query);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: error,
            msg: error ? error : networkStatus['500'],
        })
    }
})

/**
 * 分页查询订单
 * @param {*} data 
 * @returns 
 */
router.post('/query', async (req, res) => {
    try {
        const data =  await queryOrder(req.body);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    }catch (error) {
        console.log(error);
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

module.exports = router;
